﻿package{

	import flash.net.XMLSocket
	import flash.events.Event
	import flash.events.MouseEvent
	import flash.events.EventDispatcher
	import flash.events.DataEvent
	import flash.events.IOErrorEvent
	import flash.xml.XMLDocument
	import flash.xml.XMLNode
	import flash.xml.XMLNodeType
	import BlinkEvent	

	public class Blink
	{
		var Connection =  null;
		var uLists = []
		var rLists = []
		var root 
		var userName = ""
		
		function Blink(rootMovie)
		{
			root = rootMovie
			
			Connection = new BlinkClient("127.0.0.1", 5278)
			
			root.connectButton.addEventListener(MouseEvent.CLICK, function(){ 
														   	  Connection.login(root.usernameTxt.text)
															} );

			root.chatButton.addEventListener(MouseEvent.CLICK, function(){ 
																		  Connection.chat(root.chatTxt.text);
																		  root.chatTxt.text = ""
																	} );

			root.newroomButton.addEventListener(MouseEvent.CLICK, function(){ 
																		  Connection.newRoom(root.newroomTxt.text);
																	} );

			root.joinButton.addEventListener(MouseEvent.CLICK, function(){ 
																		  Connection.move(root.roomList.selectedItem.label);
																	} );
			
			
			
			Connection.addEventListener(BlinkEvent.USER_JOINS,  user_join)
			Connection.addEventListener(BlinkEvent.USER_LEAVES, user_leave)

			Connection.addEventListener(BlinkEvent.ROOM_NEW,   room_new)
			Connection.addEventListener(BlinkEvent.ROOM_CLOSE,   room_close)
			
			Connection.addEventListener(BlinkEvent.CHAT_INCOME, chat_come)
			Connection.addEventListener(BlinkEvent.LOGIN_RESULT,login_result)
			Connection.addEventListener(BlinkEvent.ROOM_INFO,   room_info)
			
			Connection.addEventListener(BlinkEvent.ERROR,   error)
		}
		
		function error( evt )
		{
			root.chatHistory.text += "Error on \""+evt.xmlNode.attributes['command']+"\":"+evt.xmlNode.attributes['error']+"\n"
		}
		
		function login_result(evt)
		{
			if(evt.attributes['result']=='pass')
			{
				root.usernameTxt.editable = false
			}else
			{
				root.chatHistory.text += "Error, Login failed."+evt.attributes['reason']
			}
		}
		
		function updateRoomList()
		{
			root.roomList.dataProvider.removeAll()
			for each(var r in rLists)
				root.roomList.dataProvider.addItem( { label: r } );

		}
		
		function room_new(evt)
		{
			for each(var r in evt.xmlNode.childNodes)
				if(r.nodeName=='room')
					rLists.push(r.attributes['name'])
			updateRoomList()
		}
		
		function room_close(evt)
		{
			for each(var r in evt.xmlNode.childNodes)
				if(r.nodeName=='room')
					rLists.splice( rLists.indexOf( r.attributes['name']),1 )
			updateRoomList()
		}		

		function updateUserList()
		{
			root.userList.text = ""
			for each(var u in uLists)
				root.userList.text += u+"\n"
		}
		
		function user_join(evt)
		{
			for each(var u in evt.xmlNode.childNodes)
				if(u.nodeName=='user')
					uLists.push(u.attributes['name'])
					if(u.attributes['name']==root.usernameTxt.text) 
						Connection.getRoomInfo()
						return
					
			updateUserList()
		}
		
		function user_leave(evt)
		{
			for each(var u in evt.xmlNode.childNodes)
				if(u.nodeName=='user')
					uLists.splice( uLists.indexOf( u.attributes['name']),1 )
			updateUserList()
		}
		
		function chat_come(evt)
		{
			root.chatHistory.text += evt.attributes['from']+":"+evt.content+"\n"
		}
		
		function room_info(evt)
		{
			for each(var n in evt.xmlNode.childNodes)
			{
				if(n.nodeName=='users')
				{
					uLists = []
					for each(var u in n.childNodes)
					{
						uLists.push(u.attributes['name'])
					}
					updateUserList()
				}
				
				if(n.nodeName=='subs')
				{
					rLists = []
					for each(var r in n.childNodes)
					{
						rLists.push(r.attributes['name'])
					}
					updateRoomList()
				}
				
			}
		}
		
	}// end class

}